//
//  RVideoItemView.m
//  iMoive
//
//  Created by 侯伟 on 2021/8/25.
//

#import "RVideoItemView.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "RVideoDetailsViewController.h"
#import "RVideoObj.h"

@interface RVideoItemView ()

/// 封面视图
@property(nonatomic,strong)UIImageView* coverImageView;

/// 名称
@property(nonatomic,strong)UILabel* nameLb;

/// 描述
@property(nonatomic,strong)UILabel* descLb;

@end

@implementation RVideoItemView

- (instancetype)init{
    if(self = [super init]){
        
        self.backgroundColor = UIColor.whiteColor;
        self.mySize = CGSizeMake(PX(105), PX(180));
        [self setTarget:self action:@selector(playMovie:)];
        
        [self initSubViews];
    }
    return self;
}


- (void)initSubViews{
    UIImageView* coverImageView = [[UIImageView alloc] init];
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    coverImageView.myHorzMargin = 0;
    coverImageView.myTop = 0;
    coverImageView.myHeight = PX(157);
    coverImageView.myWidth = PX(105);
    coverImageView.layer.cornerRadius = PX(5);
    coverImageView.layer.masksToBounds = YES;
    [self addSubview:coverImageView];
    _coverImageView = coverImageView;
    
    UILabel* descLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_WHITE text:@""];
    descLb.backgroundColor = HEXACOLOR(0xFFFFFF5F);
    descLb.textAlignment = NSTextAlignmentRight;
    descLb.bottomPos.equalTo(coverImageView.bottomPos);
    descLb.myHorzMargin = 0;
    descLb.myHeight = PX(20);
    [self addSubview:descLb];
    _descLb = descLb;
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_BLACK_DARK text:@""];
    descLb.textAlignment = NSTextAlignmentRight;
    nameLb.numberOfLines = 2;
    nameLb.topPos.equalTo(coverImageView.bottomPos).offset(4);
    nameLb.bottomPos.uBound(self.bottomPos, 0);
    nameLb.myHorzMargin = 0;
    nameLb.myHeight = MyLayoutSize.wrap;
    [self addSubview:nameLb];
    _nameLb = nameLb;
}

- (void)setMovie:(RVideoObj *)movie{
    _movie = movie;
    
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:movie.img] placeholderImage:[UIImage imageNamed:@"common_placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if(error){
            [[SDWebImageManager sharedManager] removeFailedURL:imageURL];
        }
    }];
    
    self.descLb.text = movie.info;
    [self.descLb sizeToFit];
    
    self.nameLb.text = movie.name;
    [self.nameLb sizeToFit];
}

/// 播放
- (void)playMovie:(id)sender{
    
    if(self.playBlock){
        self.playBlock(self.movie);
    }else{
        RVideoDetailsViewController* detailController = [RVideoDetailsViewController new];
        detailController.video = self.movie;
        [UIViewController showViewController:detailController animated:YES];
    }
}

@end
